// Standard includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Driverlib includes
#include "hw_types.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_common_reg.h"
#include "timer.h"
#include "interrupt.h"
#include "hw_apps_rcm.h"
#include "prcm.h"
#include "rom.h"
#include "rom_map.h"
#include "prcm.h"
#include "gpio.h"
#include "utils.h"


#include "hw_gpio.h"
#include "pin.h"

#include "gpio_if.h"
// Common interface includes

#include "pin_mux_config.h"
#include "timer_if.h"

//RGB_LED GPIO_28 GPIO_27 GPIO_26
//
//Key1    GPIO_01
//Key2    GPIO_22
//Key3    GPIO_23
//Key4    GPIO_24


//
//
#if defined(ccs)
extern void (* const g_pfnVectors[])(void);
#endif
#if defined(ewarm)
extern uVectorEntry __vector_table;
#endif

unsigned long keyflag=1;
int state=1;
unsigned long g_ulBase = TIMERA0_BASE;
//*****************************************************************************
//
//! Board Initialization & Configuration
//!
//! \param  None
//!
//! \return None
//
//*****************************************************************************
static void
BoardInit(void)
{
	/* In case of TI-RTOS vector table is initialize by OS itself */
	#ifndef USE_TIRTOS
	  //
	  // Set vector table base
	  //
	#if defined(ccs)
	    MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
	#endif
	#if defined(ewarm)
	    MAP_IntVTableBaseSet((unsigned long)&__vector_table);
	#endif
	#endif
    //
    // Enable Processor
    //
    MAP_IntMasterEnable();
    MAP_IntEnable(FAULT_SYSTICK);

    PRCMCC3200MCUInit();
}

void Key3Handler(void)
{
    unsigned long ulPinState =  GPIOIntStatus(GPIOA2_BASE,1);
    if(ulPinState & GPIO_INT_PIN_7)
    {
    	GPIOIntClear(GPIOA2_BASE,GPIO_INT_PIN_7);
        keyflag++;
    }
}

void Key4Handler(void)
{
    unsigned long ulPinState =  GPIOIntStatus(GPIOA3_BASE,1);
    if(ulPinState & GPIO_INT_PIN_0)
    {
    	GPIOIntClear(GPIOA3_BASE,GPIO_INT_PIN_0);
       if(keyflag!=1)
    	keyflag--;
       else
    	   keyflag=1;
    }

}
void TimerBaseIntHandler (void)
{
	state=~state;
	GPIOPinWrite(GPIOA3_BASE,GPIO_PIN_4 | GPIO_PIN_3 | GPIO_PIN_2,state);
	Timer_IF_ReLoad(g_ulBase, TIMER_A, keyflag*500);
}
void Keyinit(void)
{
	GPIOIntTypeSet(GPIOA2_BASE,GPIO_PIN_7,GPIO_RISING_EDGE);
	IntPrioritySet(INT_GPIOA2, INT_PRIORITY_LVL_1);
	GPIOIntRegister(GPIOA2_BASE,Key3Handler);
	GPIOIntClear(GPIOA2_BASE,GPIO_INT_PIN_7);
    IntPendClear(INT_GPIOA2);
    IntEnable(INT_GPIOA2);
	GPIOIntEnable(GPIOA2_BASE,GPIO_INT_PIN_7);

	GPIOIntTypeSet(GPIOA3_BASE,GPIO_PIN_0,GPIO_RISING_EDGE);
	IntPrioritySet(INT_GPIOA3, INT_PRIORITY_LVL_1);
	GPIOIntRegister(GPIOA3_BASE,Key4Handler);
	GPIOIntClear(GPIOA3_BASE,GPIO_INT_PIN_0);
    IntPendClear(INT_GPIOA3);
    IntEnable(INT_GPIOA3);
	GPIOIntEnable(GPIOA3_BASE,GPIO_INT_PIN_0);

}

 /*
 *
 * main.c
 */
void main(void) {
	
    //
    // Initialize Board configurations
    //
    BoardInit();

    //
    // Power on the corresponding GPIO port B for 9,10,11.
    // Set up the GPIO lines to mode 0 (GPIO)
    //
    PinMuxConfig();

    //button init
    Keyinit();

   // GPIOPinWrite(GPIOA3_BASE,GPIO_PIN_4 | GPIO_PIN_3 | GPIO_PIN_2,~state);
    GPIO_IF_LedConfigure(LED1|LED3|LED2);

    GPIO_IF_LedOff(MCU_RED_LED_GPIO);
    GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);
    GPIO_IF_LedOff(10);
      //
      // Base address for second timer
      //
   //   g_ulRefBase = TIMERA1_BASE;
      //
      // Configuring the timers
      //
//      Timer_IF_Init(PRCM_TIMERA0, g_ulBase, TIMER_CFG_PERIODIC, TIMER_A, 0);
 //     Timer_IF_Init(PRCM_TIMERA1, g_ulRefBase, TIMER_CFG_PERIODIC, TIMER_A, 0);

      //
      // Setup the interrupts for the timer timeouts.
      //
//      Timer_IF_IntSetup(g_ulBase, TIMER_A, TimerBaseIntHandler);
     // Timer_IF_IntSetup(g_ulRefBase, TIMER_A, TimerRefIntHandler);

      //
      // Turn on the timers feeding values in mSec
      //
//      Timer_IF_Start(g_ulBase, TIMER_A, keyflag*500);
    //  Timer_IF_Start(g_ulRefBase, TIMER_A, 1000);

	while(1)
	{

		 GPIO_IF_LedToggle(MCU_RED_LED_GPIO);
		UtilsDelay(keyflag*1000000);
	}
}
